package com.tiankong.product.controller;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tiankong.product.entity.AttrGroup;
import com.tiankong.product.entity.Category;
import com.tiankong.product.service.AttrGroupService;
import com.tiankong.product.service.CategoryService;
import net.youdi.common.model.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 属性分组 前端控制器
 * </p>
 *
 * @author scl
 * @since 2021-03-22
 */
@RestController
@RequestMapping("/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 保存
     */
    @RequestMapping("/save")
    public Result save(@RequestBody AttrGroup attrGroup){
        attrGroupService.saveOrUpdate(attrGroup);
        return Result.ok();
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public Result info(@PathVariable("id") Long id){
        AttrGroup attrGroup = attrGroupService.getById(id);

        Long catelogId = attrGroup.getCatelogId();
        Long[] path = categoryService.findCatelogPath(catelogId);

        attrGroup.setCatelogPath(path);

        return Result.ok(attrGroup);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public Result list(
            @RequestParam(value = "page",required = false,defaultValue = "1") Integer pageIndex,
            @RequestParam(value = "limit",required = false,defaultValue = "10") Integer limit,
            @RequestParam(value = "key",required = false) String key,
            @PathVariable("catelogId") Long catelogId){
        QueryChainWrapper<AttrGroup> query = attrGroupService.query();

        if(!StringUtils.isEmpty(key)){
            query.and((obj)-> obj.eq("attr_group_id",key).or().like("attr_group_name",key));
        }
        IPage<AttrGroup> page = new Page<>(pageIndex,limit);
        if( catelogId != 0){
            query.eq("catelog_id",catelogId);
        }
        page = query.page(page);
        List<AttrGroup> attrGroups = page.getRecords();
        if (CollectionUtil.isNotEmpty(attrGroups)){
            Map<Long, List<AttrGroup>> group = attrGroups.stream().collect(Collectors.groupingBy(AttrGroup::getCatelogId));
            List<Category> categories = categoryService.listByIds(group.keySet());

            for (Category category : categories) {
                List<AttrGroup> list = group.get(category.getCatId());
                for (AttrGroup attrGroup : list) {
                    attrGroup.setCatName(category.getName());
                }
            }
        }
        return Result.ok().setPage(page.getTotal(),attrGroups);
    }

    /**
     * 列表
     */
    @RequestMapping("/withattr/{catelogId}")
    public Result withAttr(@PathVariable("catelogId") Long catelogId){

        List<AttrGroup> attrGroups = attrGroupService.listWithAttrByCatId(catelogId);
        return Result.ok(attrGroups);
    }

    @DeleteMapping("delete")
    public Result delete(@RequestBody Set<Integer> ids){
        attrGroupService.batchDelete(ids);
        return Result.ok();
    }
}
